﻿using System;
using System.Drawing;
using System.Web.Security;
using System.Web.UI;
using AjaxControlToolkit;
using TeamFranceTriple.TftWcfProxy;
using FranceTriple.Dal;
using System.Web.UI.WebControls;

namespace TeamFranceTriple.Account
{
    public partial class UserProfileControl : BaseUserControl
    {
        public string UserAddressIp { get; set; }
        private UserProfile profile;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (TimeZoneDropDownList.Items.Count == 0)
            {
                foreach (var tz in TimeZoneInfo.GetSystemTimeZones())
                    TimeZoneDropDownList.Items.Add(new ListItem() { Value = tz.Id, Text = tz.DisplayName });
            }

            if (ThisMaster.Requester.Membership.PasswordQuestion == null)
            {
                MessageLabel.Visible = true;
                QuestionAnswerButton.ForeColor = Color.Red;
            }
            if (ThisMaster.Requester.Profile.UserBirthday == new DateTime())
                CalendarExtender1.DefaultView = CalendarDefaultView.Years;

            LoadDataInControl();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {

        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            LoadDataInControl();
        }

        private void LoadDataInControl()
        {
            EmailTextBox.Text = ThisMaster.Requester.Membership.Email.HtmlToText();
            UserChoosenCultureDropDownList.SelectedValue = ThisMaster.Requester.Profile.UserChoosenCulture;
            UserThemeDropDownList.SelectedValue = ThisMaster.Requester.Profile.UserTheme;
            CalendarExtender1.SelectedDate = (ThisMaster.Requester.Profile.UserBirthday == new DateTime()) ? DateTime.Now : ThisMaster.Requester.Profile.UserBirthday;
            LocationTextBox.Text = ThisMaster.Requester.Profile.Location.HtmlToText();
            TimeZoneDropDownList.SelectedValue = ThisMaster.Requester.Profile.TimeZone;

            Bf3ColonelNicknameTextBox.Text = ThisMaster.Requester.Profile.Bf3ColonelNickname.HtmlToText();
            TimePlayedToBecomeBf3ColonelTextBox.Text = ThisMaster.Requester.Profile.TimePlayedToBecomeBf3Colonel.ToString();
            Bf3ColonelDisplayOrderTextBox.Text = ThisMaster.Requester.Profile.Bf3ColonelDisplayOrder.ToString();
            UserGravatarEmailTextBox.Text = ThisMaster.Requester.Profile.UserGravatarEmail.HtmlToText();

            UpperLabelSignatureTextBox.Text = ThisMaster.Requester.Profile.UpperLabelSignature.HtmlToText();
            ImageUrlSignatureTextBox.Text = ThisMaster.Requester.Profile.ImageUrlSignature;
            NavigateUrlSignatureTextBox.Text = ThisMaster.Requester.Profile.NavigateUrlSignature;
            BottomLabelSignatureTextBox.Text = ThisMaster.Requester.Profile.BottomLabelSignature.HtmlToText();

            Bf3RankImage1.ImageUrl = ThisMaster.Requester.Profile.Bf3RankImage;
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (!this.Page.IsValid) return;

            DateTime userBirthDate;
            ThisMaster.Requester.Profile.UserChoosenCulture = UserChoosenCultureDropDownList.SelectedItem.Value;
            ThisMaster.Requester.Profile.UserTheme = UserThemeDropDownList.SelectedItem.Value;

            bool res = DateTime.TryParse(UserBirthDayTextBox.Text, out userBirthDate);
            if (res && userBirthDate < DateTime.Now.Date)
            {
                ThisMaster.Requester.Profile.UserBirthday = userBirthDate;
            }

            ThisMaster.Requester.Profile.Location = LocationTextBox.Text.TextToHtml();
            ThisMaster.Requester.Profile.TimeZone = TimeZoneDropDownList.SelectedValue;
            ThisMaster.Requester.Profile.Bf3ColonelNickname = Bf3ColonelNicknameTextBox.Text.TextToHtml();
            ThisMaster.Requester.Profile.TimePlayedToBecomeBf3Colonel = Int32.Parse(TimePlayedToBecomeBf3ColonelTextBox.Text);
            ThisMaster.Requester.Profile.Bf3ColonelDisplayOrder = Int32.Parse(Bf3ColonelDisplayOrderTextBox.Text);
            ThisMaster.Requester.Profile.UserGravatarEmail = UserGravatarEmailTextBox.Text.TextToHtml();
            ThisMaster.Requester.Profile.UpperLabelSignature = UpperLabelSignatureTextBox.Text.TextToHtml();
            ThisMaster.Requester.Profile.ImageUrlSignature = ImageUrlSignatureTextBox.Text;
            ThisMaster.Requester.Profile.NavigateUrlSignature = NavigateUrlSignatureTextBox.Text;
            ThisMaster.Requester.Profile.BottomLabelSignature = BottomLabelSignatureTextBox.Text.TextToHtml();
            ThisMaster.Requester.Profile.InitialAddressIp = this.UserAddressIp;

            //Mise à jour du profile
            profile = UserProfile.GetUserProfile();
            profile = ThisMaster.Requester.Profile;
            profile.Save();

            //Mise à jour du membership
            ThisMaster.Requester.Membership.Email = EmailTextBox.Text;
            Membership.UpdateUser(ThisMaster.Requester.Membership);

            // La mise à jour du cache avec les nouvelles informations se fait toute seul car ThisMaster.Requester point sur Session["Requester"]

            // Mise à jour du cache des anniversaires : elle se fait toute seule par SqlCacheDependency
            //Cache.Remove("HappyBirthdayCacheKey");
            //Cache.Remove("NextUserBirthdayCacheKey");

            Response.Redirect("~/" + ThisMaster.Requester.Profile.UserChoosenCulture + "/");
        }

        protected void ChangePasswordButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/" + ThisMaster.Requester.Culture + "/changepassword");
        }

        protected void TestSignatureButton_Click(object sender, EventArgs e)
        {
            ForumSignature1.UpperText = UpperLabelSignatureTextBox.TextToHtml();
            ForumSignature1.ImageUrl = ImageUrlSignatureTextBox.Text;
            ForumSignature1.NavigateUrl = NavigateUrlSignatureTextBox.Text;
            ForumSignature1.BottomText = BottomLabelSignatureTextBox.TextToHtml();
            ForumSignature1.Visible = true;
        }

        protected void GetBf3RankButton_Click(object sender, EventArgs e)
        {
            Int32 NewRankFromWeb;

            TftWcfClient proxy = new TftWcfClient();
            proxy.Open();
            NewRankFromWeb = proxy.GetBf3RankByBf3Playername(Bf3ColonelNicknameTextBox.Text.TextToHtml());
            proxy.Close();

            if (NewRankFromWeb > 0)
            {
                //Empecher de :  choisir un pseudo bf3 pour avoir le grade, puis mettre un autre pseudo
                ThisMaster.Requester.Profile.Bf3ColonelNickname = Bf3ColonelNicknameTextBox.Text.TextToHtml();
                ThisMaster.Requester.Profile.Bf3Rank = NewRankFromWeb;
                Bf3RankImage1.ImageUrl = ThisMaster.Requester.Profile.Bf3RankImage;
            }
        }

        protected void Bf3ColonelNicknameTextBox_TextChanged(object sender, EventArgs e)
        {
            //Empecher de :  choisir un pseudo bf3 pour avoir le grade, puis mettre un autre pseudo
            ThisMaster.Requester.Profile.Bf3Rank = 0;
        }

        protected void Button_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/" + ThisMaster.Requester.Culture + "/changepasswordquestionanswer");
        }

    }
}